<?php
namespace Com\Pay;
// 杉得公众号支付
class ShandeGzhPay{

    private $config = array();

  
    public function __construct($send_data){
        $this->config = $send_data;
    }


    /*生成支付连接*/
    public function getUrl($order_info,$other = array()){
        /* 获取提交的订单交易金额 */
        $price = $order_info['paymoney'];
        //支付成功后跳转地址
        if(!empty($other['returl'])){
            $returnUrl = $other['returl'];
        }else{
            $returnUrl    = $order_info['return_url'];
        }
        if(!empty($other['callback'])){
            $notifyUrl = $other['callback'];
        }else{
            /* 获取提交的后台回调地址 */
            $notifyUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/Api/NotifyCallback/ShandePayNotify';
        }
        $order_sn = $order_info['ordernumber'];
        $Basedata = array(
            'app_id' => $this->config['mch_id'],
            'method' => 'openapi.payment.order.h5pay',
            'nonce' => rand(111111,999999),
            'format' => 'json',
            'sign_method' => 'md5',
            'biz_content' => NULL,
        );

        $post_data = array(
            'merchant_order_sn' => $order_sn,
            'open_id' => $order_info['openid'],
            'sub_open_id' => $order_info['sub_open_id'],
            'total_fee' => $price,
        );
        $Basedata['biz_content'] = json_encode($post_data);
        $ContentSign = ShandeGzhPay::ShandeSetSign($Basedata , $this->config['mch_key'],false); // 计算出Sign
        $Basedata['sign'] = $ContentSign; // Sign签名
        $SendUrl = 'https://shq-api.51fubei.com/gateway';
        $res = json_decode($this->https_request($SendUrl,json_encode($Basedata)) , true);
        var_dump($res);die;
        $payInfo = $res['data'];
        if($payInfo['prepay_id'] != '') {
            $PayUrl = 'https://shq-api.51fubei.com/paypage?prepay_id=' . $payInfo['prepay_id'] . '&callback_url=' . $order_info['return_url'] . '&cancel_url=' . $order_info['return_url'];
            return $PayUrl;
        }
        return '';
    }

    public static function ShandeSetSign($data , $app_secret ,  $ISNotify = false){
        $str = "";
        ksort($data);
        foreach($data as $key1=>$val)  {
            if($ISNotify && $key1 == 'sign') continue;
            $str.=$key1."=".$val."&";
        }
        $str = rtrim($str,'&');
        $sign = strtoupper(md5($str . $app_secret));
        return $sign;
    }

    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $a  = file_get_contents('php://input');
        $data = $this->xmlToArray($a);
        $MySign = $this->setParam($data,$this->config['mch_key'],true);
        if($data['sign'] == $MySign){
            if($data['pay_result'] == '0' && $data['status'] == '0'){
                return true;
            }else{
                // 失败
                return false;
            }
        }else{
            // 失败
            return false;
        }
    }


    private function https_request($url, $data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        //参数不为空则为post请求

        if (!empty($data)){

            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }


}
